package com.example.jeong.yuejian10.Activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.jeong.yuejian10.Adapter.CommentListAdapter;
import com.example.jeong.yuejian10.Bean.CommentBean;
import com.example.jeong.yuejian10.Bean.PostBean;
import com.example.jeong.yuejian10.Bean.UserBean;
import com.example.jeong.yuejian10.Model.CommentModel;
import com.example.jeong.yuejian10.Model.PostModel;
import com.example.jeong.yuejian10.Model.UserModel;
import com.example.jeong.yuejian10.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.jeong.yuejian10.constant.Constant.POST_PHOTO_URL;
import static com.example.jeong.yuejian10.constant.Constant.USER_PHOTO_URL;

//帖子详情页
public class PostDetailActivity extends Activity {

    private ImageView backlist;
    // private Button collect;
    protected Context context;
    private ImageView imagepost;
    private TextView postinfo;
    private TextView posttitle;
    private TextView postusername;
    private ImageButton postuserpic;
    private TextView posttime;
    private EditText addcomment;
    private Button putcomment;
    private String AddComment;

    private int p_id;


    private int usertid;

    private RecyclerView recyclerView;
    private List mDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);
        p_id = getIntent().getIntExtra("p_id", -1);
        usertid = getIntent().getIntExtra("u_id", -1);
        initViews();
        initEvent();
        getData();
    }

    private void initViews() {
        backlist = (ImageView) findViewById(R.id.back_postlist);
        //  collect = (Button) findViewById(R.id.collect);
        imagepost = (ImageView) findViewById(R.id.image_post);
        postinfo = (TextView) findViewById(R.id.post_infod);
        posttitle = (TextView) findViewById(R.id.post_titled);
        posttime = (TextView) findViewById(R.id.post_timed);
        postusername = (TextView) findViewById(R.id.post_usernamed);
        postuserpic = (ImageButton) findViewById(R.id.post_userpicture);
        recyclerView = (RecyclerView) findViewById(R.id.rV_comment);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        addcomment = (EditText) findViewById(R.id.AddComment);
        putcomment = (Button) findViewById(R.id.PutComment);
    }

    private void getData(){
        Bundle bundle=getIntent().getExtras();
        int p_id=bundle.getInt("p_id");
        CommentModel commentModel = new CommentModel();
        Call<List<CommentBean>> call=commentModel.getCommentList(p_id);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                List<CommentBean> data =(List<CommentBean>) response.body();
                System.out.printf("data");
                recyclerView.setAdapter(new CommentListAdapter(PostDetailActivity.this,data));
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                Toast.makeText(PostDetailActivity.this, "fail", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initEvent() {
        postuserpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(PostDetailActivity.this, UserPostActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("u_id",usertid);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);

            }
        });

        putcomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                usertid = sp.getInt("u_id", 0);
                // Log.d("u","usertid");
                AddComment = addcomment.getText().toString();

                Bundle bundle = getIntent().getExtras();
                int postid = bundle.getInt("p_id");

                CommentModel commentModel = new CommentModel();
                Call<CommentBean> call = commentModel.commentPost(postid,usertid,AddComment);
                call.enqueue(new Callback<CommentBean>() {
                    @Override
                    public void onResponse(Call<CommentBean> call, Response<CommentBean> response) {
                        CommentBean data = (CommentBean) response.body();  //获取数据
                        if (data.getSuccess().equals("1")) {
                            Toast.makeText(PostDetailActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                            finish();

                        } else {
                            Toast.makeText(PostDetailActivity.this, "添加失败", Toast.LENGTH_LONG).show();

                        }
                    }

                    @Override
                    public void onFailure(Call<CommentBean> call, Throwable t) {

                    }
                });
            }
        });


        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        PostModel postModel = new PostModel();
        Call<PostBean> callp = postModel.getDetail(p_id,usertid);
        callp.enqueue(new Callback<PostBean>() {
            @Override
            public void onResponse(Call<PostBean> call, Response<PostBean> response) {
                String postPic = POST_PHOTO_URL + response.body().getP_pic();
                Picasso.with(context)
                        .load(postPic)
                        .placeholder(R.drawable.ic_add)
                        .error(R.drawable.ic_add)
                        .into(imagepost);

                postinfo.setText(response.body().getP_info());
                posttitle.setText(response.body().getP_title());
                posttime.setText(response.body().getP_createtime());

            }

            @Override
            public void onFailure(Call<PostBean> call, Throwable t) {

            }
        });


        UserModel userModel = new UserModel();
        Call<UserBean> callu = userModel.UserInfo(usertid);
        callu.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                postusername.setText(response.body().getU_name());

                String userPic = USER_PHOTO_URL + response.body().getU_pic();
                Picasso.with(context)
                        .load(userPic)
                        .placeholder(R.drawable.ic_add)
                        .error(R.drawable.ic_add)
                        .into(postuserpic);


            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });

//    private void getPostDetail(){
//
//            Bundle bundle = this.getIntent().getExtras();
//            int postid = bundle.getInt("p_id");
//            //  Log.d("m","p_id");
//            String postInfo = bundle.getString("p_info");
//            String postTitle = bundle.getString("p_title");
//            String postTime = bundle.getString("p_createtime");
//            String PostPic = POST_PHOTO_URL+bundle.getString("p_pic");
//
//            postinfo.setText(postInfo);
//            posttitle.setText(postTitle);
//            posttime.setText(postTime);
//            Picasso.with(this).load(PostPic).into(imagepost);
//
//    }


    }
}

